package algorithm.avl;

import algorithm.search.BinarySearchTree;
import algorithm.trie.FileOperation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author bingo
 * @Description      compare  BST And  AVL
 * @Date 2018/7/14
 */
public class Main {

    public static void main(String[] args) {

        List<String> words = new ArrayList<>();
        FileOperation.readFile("demo/pride-and-prejudice.txt", words);
        //Collections.sort(words);
        BinarySearchTree bst = new BinarySearchTree();
        AVLTree avlTree = new AVLTree();

        long startTime = System.nanoTime();
        for (String word : words) {
            if (bst.contain(word)) {
                bst.set(word, (int) bst.get(word) + 1);
            } else {
                bst.insert(word, 1);
            }

        }
        long endTime = System.nanoTime();

        double time = (endTime-startTime)/1000000000.0;

        System.out.println("bst:"+time+"s");
        System.out.println("Frequency of PRIDE: " + bst.get("pride"));
        System.out.println("Frequency of PREJUDICE: " + bst.get("prejudice"));

        startTime = System.nanoTime();
        for (String word : words) {
            if (avlTree.contains(word)) {
                avlTree.set(word, (int) bst.get(word) + 1);
            } else {
                avlTree.insert(word, 1);
            }

        }
        endTime = System.nanoTime();
        time = (endTime-startTime)/1000000000.0;
        System.out.println("Total different words: " + avlTree.getSize());
        System.out.println("avl：" + time+"s");
        System.out.println("Frequency of PRIDE: " + avlTree.get("pride"));
        System.out.println("Frequency of PREJUDICE: " + avlTree.get("prejudice"));
    }

}
